package com.example.fridaykt.controller

import com.example.fridaykt.common.base.BaseController
import com.example.fridaykt.common.result.R
import com.example.fridaykt.model.SysMenu
import com.example.fridaykt.repository.SysMenuRepository
import com.example.fridaykt.service.SysMenuService
import com.example.fridaykt.vo.Page
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.*

@RestController
@Tag(name = "菜单管理接口")
class MenuController(
    private val menuService: SysMenuService,
    private val sysMenuRepository: SysMenuRepository,
) : BaseController() {

    @Operation(summary = "根据角色获取菜单")
    @GetMapping("/menu/list")
    fun menuListByRole(): R {
        val id: Long = getLoginUserId()
        val list: List<SysMenu> = menuService.listWithTreeWithRole(id)
        return R.success(list)
    }


    @GetMapping("/menu")
    @Operation(summary = "获取所有菜单")
    fun getMenuList(
        @RequestParam(defaultValue = "1") current: Int,
        @RequestParam(defaultValue = "10") pageSize: Int
    ): R {
        val page: Page<SysMenu> = menuService.getMenuList(current, pageSize)
        return R.success(page)
    }

    @PostMapping("/menu")
    fun add(@RequestBody menu: SysMenu): R {
        sysMenuRepository.save(menu)
        return toAjax(1)
    }

    @PutMapping("/menu")
    fun update(@RequestBody menu: SysMenu): R {
        sysMenuRepository.update(menu)
        return toAjax(1)
    }

    @DeleteMapping("/menu")
    fun delete(id: Long): R {
        sysMenuRepository.deleteById(id)
        return toAjax(1)
    }
}
